package com.example.campustalent.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter

class TabPagerAdapter : FragmentPagerAdapter {
    private var list: List<Fragment>
    private var titleString: Array<String>? = null
    private var titleList: MutableList<String>? = null

    constructor(fm: FragmentManager?, list: List<Fragment>, titles: Array<String>?) : super(
        fm!!
    ) {
        this.list = list
        titleString = titles
    }

    constructor(fm: FragmentManager?, list: List<Fragment>, titles: MutableList<String>?) : super(
        fm!!
    ) {
        this.list = list
        titleList = titles
    }

    override fun getItem(position: Int): Fragment {
        return list[position]
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        if (titleString != null && titleString!!.isNotEmpty()) {
            return titleString!![position]
        }
        return if (titleList != null && titleList!!.isNotEmpty()) {
            titleList!![position]
        } else ""
    }

    fun setPageTitle(position: Int, title: String) {
        if (titleString != null && titleString!!.isNotEmpty()) {
            if (position >= 0 && position < titleString!!.size) {
                titleString!![position] = title
                notifyDataSetChanged()
            }
        } else if (titleList != null && titleList!!.isNotEmpty()) {
            if (position >= 0 && position < titleList!!.size) {
                titleList!![position] = title
                notifyDataSetChanged()
            }
        }
    }
}